$(function(){
	
	//提交表单前验证
	$('#submit').click(function() {
		event.preventDefault()
		let phone = $('#phone').val();
		let password = $('#password').val();
		let newpassword = $('#newpassword').val();
		let againpassword = $('#againpassword').val();
		let flag = true;
		 if (password == "") {
			$('#password_trip').show();
			flag = false;
		} else if(newpassword == ""){
			$('#newpassword_trip').show();
			flag = false;
		}else if(againpassword == ""){
			$('#againpassword_trip').show();
			flag = false;
		}else if(againpassword!=newpassword){
			flag = false;
		}else if(newpassword.length < 8 || /^\d+$/.test(newpassword)){
			flag = false;
		}else{
			
			$.ajax({
				url: "v1/users/updatepassword",
				type: "put",
				async: false,
				data: {
					password: $("#password").val(),
					newpassword: $("#newpassword").val(),
				},
				success: function(data) {
					
					if (data.status == 200){
						alert('密码修改成功');
						window.location = '/homePage'
					}else{
						alert('密码修改失败');
					}
					
				},
				error: function(err) {
					console.log(err);
					alert('密码修改失败');
				},
			});
			
		}
		
		return flag;
})


//重置
	// $('#reset').click(function(){
	// 	let phone = $('#phone').val();
	// 	let password = $('#password').val();
	// 	let newpassword = $('#newpassword').val();
	// 	let againpassword = $('#againpassword').val();
	// 	phone=="";
	// 	password=="";
	// 	newpassword=="";
	// 	againpassword=="";
	// })
	
	
	
	
	
	//输入框改变
	
	$('#password').change(function() {
		if($('#password').val()==""){
			$('#password_trip').show();
			$('#password_check').hide();
			$('#password_check1').hide();
		}else{
			$('#password_trip').hide();
			$('#password_check').hide();
			$('#password_check1').hide();
			$.ajax({
				url: "v1/users/checkpassword",
				type: "post",
				data: {
					password: $('#password').val()
				},
				success: function(data) {
					if (data.status == 200) {
						$('#password_check').show();
						$('#password_check1').hide();
					} else {
						$('#password_check1').show();
						$('#password_check').hide();
					}
				},
				error: function(err) {
					console.log(err)
				},
			});
		}
	})
	
	$('#newpassword').change(function() {
		let newpassword=$('#newpassword').val();
		if(newpassword==""){
			$('#newpassword_warn').hide();
			$('#newpassword_trip').show();
		}else{
			$('#newpassword_trip').hide();
		}
		
		if ((newpassword.length < 8 || /^\d+$/.test(newpassword)) && newpassword!="") { //判断是否为数字
			$('#newpassword_warn').show();
			return false;
		} else {
			$('#newpassword_warn').hide();
		}
	})
	
	$('#againpassword').change(function() {
		let newpassword = $('#newpassword').val();
		let againpassword = $('#againpassword').val();
		if(againpassword==""){
			$('#againpassword_trip').show();
			$('#againpassword_error').hide();
		}else{
			$('#againpassword_trip').hide();
		}

		
		if(againpassword!=newpassword &&againpassword!=""){
			$('#againpassword_error').show();
		}else{
			$('#againpassword_error').hide();
		}
		
	})

$('.js-btn').click(function(){
		window.history.go(-1)
	})
})